﻿using Newtonsoft.Json;
using QD.LopPlat.UI.Code.DbContent;
using QD.LopPlat.UI.Model.Sys;
using QD.SQLHelper;
using QD.UpDownLoad;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using static QD.UpDownLoad.WebClientHelper;

namespace QD.LopPlat.UI.Code
{
    public static class QDUserHelper
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static QDUser LoginQdUser;

        /// <summary>
        /// 取当前登录用户的角色集合
        /// </summary>
        /// <returns></returns>
        public static List<string> GetCurRoles()
        {
            List<string> roleList = LoginQdUser.UType.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            for (int i = 0; i < roleList.Count; i++)
            {
                roleList[i] = roleList[i].Trim();
            }
            return roleList;
        }

        /// <summary>
        /// 消除登录状态
        /// </summary>
        public static void ClearLoginStatus()
        {
            DbConfig.Instance.LogonConfig.ServerName = "";
            DbConfig.Instance.LogonConfig.UserID = "";
            DbConfig.Instance.LogonConfig.PassWord = "";
            QDUserHelper.LoginQdUser = null;
        }

        /// <summary>
        /// 登录用户
        /// </summary>
        /// <param name="_qdUid"></param>
        /// <param name="_qdPwd"></param>
        /// <param name="_cbSource"></param>
        /// <param name="_uid"></param>
        /// <param name="_pwd"></param>
        /// <param name="_logonType"></param>
        /// <returns></returns>
        public static bool LoginDb(string _cbSource, string _uid, string _pwd, int _logonType)
        {
            DbConfig.Instance.DbDataBaseName = "QDDevPlat_V4";
            DbConfig.Instance.DbResourcesName = "QDResources";   
            DbConfig.Instance.LogonConfig.ServerName = _cbSource;
            DbConfig.Instance.LogonConfig.UserID = _uid;
            DbConfig.Instance.LogonConfig.PassWord = _pwd;
            DbConfig.Instance.LogonConfig.LogonType = _logonType;

            string errorMsg = string.Empty;
            MSDbContext db = new MSDbContext(DbConfig.Instance.GetDbDataBaseConnStr());

            if (!db.IsConnection(ref errorMsg))
            {
                Tools.PromptHelper.Prompt("连接组织数据库失败！\r\n" + errorMsg);
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
